package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsDataDictExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsDataDictMapper;
import cn.chiship.framework.upms.biz.system.service.UpmsDataDictService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.upms.biz.system.entity.UpmsDataDict;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UpmsDataDictService实现
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsDataDictServiceImpl extends BaseServiceImpl<UpmsDataDict, UpmsDataDictExample> implements UpmsDataDictService {


    @Resource
    UpmsDataDictMapper upmsDataDictMapper;

    /**
     * 新增字典
     */
    @Override
    public BaseResult insertSelective(UpmsDataDict upmsDataDict) {
        UpmsDataDictExample dataDictExample = new UpmsDataDictExample();
        UpmsDataDictExample.Criteria criteria = dataDictExample.createCriteria();
        criteria.andDataDictNameEqualTo(upmsDataDict.getDataDictCode());
        criteria.andPidEqualTo(upmsDataDict.getPid());
        String tip = "字典组编码已存在，请重新输入";
        if (String.valueOf(BaseConstants.NO).equals(upmsDataDict.getPid())) {
            tip = "字典编码已存在，请重新输入";
        }
        List<UpmsDataDict> upmsDataDicts = upmsDataDictMapper.selectByExample(dataDictExample);
        if (upmsDataDicts.isEmpty()) {
            return super.insertSelective(upmsDataDict);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, tip);
        }
    }

    /**
     * 更新字典
     */
    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsDataDict upmsDataDict) {
        UpmsDataDictExample dataDictExample = new UpmsDataDictExample();
        UpmsDataDictExample.Criteria criteria = dataDictExample.createCriteria();
        criteria.andDataDictNameEqualTo(upmsDataDict.getDataDictCode());
        criteria.andPidEqualTo(upmsDataDict.getPid());
        criteria.andIdNotEqualTo(upmsDataDict.getId());
        String tip = "字典组编码已存在，请重新输入";
        if (String.valueOf(BaseConstants.NO).equals(upmsDataDict.getPid())) {
            tip = "字典编码已存在，请重新输入";
        }
        List<UpmsDataDict> upmsDataDicts = upmsDataDictMapper.selectByExample(dataDictExample);
        if (upmsDataDicts.isEmpty()) {
            return super.updateByPrimaryKeySelective(upmsDataDict);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, tip);
        }
    }
}
